<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\message\Category;
use app\common\model\message\Message as MessageMessage;
use app\common\model\message\Review;

/**
 * 信息接口
 */
class Message extends Api
{
    protected $noNeedLogin = ['lists', 'info'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (信息分类)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function category()
    {
        $userinfo = $this->auth->getUser();
        $model = new Category;
        if ($userinfo['group_id']) {
            $model->where('find_in_set(' . $userinfo['group_id'] . ',group_ids)');
        }
        if ($userinfo['firm_id']) {
            $model->where('find_in_set(' . $userinfo['firm_id'] . ',firm_ids)');
        }
        $model->whereOr(function ($q) {
            $q->whereOr('group_ids', null);
            $q->whereOr('group_ids', '');
            $q->whereOr('firm_ids', null);
            $q->whereOr('firm_ids', '');
        });
        $list = $model->select();
        $this->success('请求成功', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (信息列表)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="分类")
     * @ApiParams (name="keywords", type="string", required=true, sample="", description="关键字")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $param = request()->param();
        extract($param);
        $userinfo = $this->auth->getUser();
        $model = new MessageMessage();
        if (isset($keywords) && !empty($keywords)) {
            $model->whereLike('title', '%' . $keywords . '%');
        }

        if (!isset($id) || !$id) {
            $category = new Category;
            if ($userinfo['group_id']) {
                $category->where('find_in_set(' . $userinfo['group_id'] . ',group_ids)');
            }
            if ($userinfo['firm_id']) {
                $category->where('find_in_set(' . $userinfo['firm_id'] . ',firm_ids)');
            }
            $category->whereOr(function ($q) {
                $q->whereOr('group_ids', null);
                $q->whereOr('group_ids', '');
                $q->whereOr('firm_ids', null);
                $q->whereOr('firm_ids', '');
            });
            $cids = $category->column('id');
            $model->whereIn('category_id', $cids);
        } else {
            $model->where('category_id', $id);
        }
        $lists = $model->where('status','normal')->paginate();
        $this->success('请求成功', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (信息详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $param = request()->param();
        extract($param);
        $data = MessageMessage::get($id);
        $this->success('请求成功', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (评论列表)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="pid", type="integer", required=true, sample="", description="评论ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function review_list()
    {
        $param = request()->param();
        extract($param);
        $model = new Review();
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        if (!isset($pid) || !$pid) {
            $pid = 0;
        }
        $list = $model->with(['user'])->where('p_id', $pid)->where('m_id', $id)->where('switch', 1)->order('id desc')->paginate();
        $this->success('请求成功', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (评论发布/评论回复)
     * @ApiMethod (POST)
     * @ApiParams (name="m_id", type="integer", required=true, sample="", description="信息ID")
     * @ApiParams (name="p_id", type="integer", required=true, sample="", description="评论ID（回复必填）")
     * @ApiParams (name="content", type="integer", required=true, sample="", description="评论内容")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function pub_review()
    {
        $param = request()->param();
        extract($param);
        $param['user_id'] = $this->auth->id;
        $model = new Review($param);
        $res = $model->allowField(true)->save();
        if ($res) {
            $this->success('成功');
        }
        $this->error('失败');
    }
}
